package implements

import (
	"gitee.com/bitwormhole/cloud-aliyun-api/aliyun/openapi"
	"github.com/starter-go/httpagent"
)

// AliyunOpenAPIService ...
type AliyunOpenAPIService struct {

	//starter:component

	_as func(openapi.Service) //starter:as("#")

	HTTPClients httpagent.Clients //starter:inject("#")

	DefaultClientEndpoint        string //starter:inject("${aliyun.open-api-client.default.endpoint}")
	DefaultClientAccessKeyID     string //starter:inject("${aliyun.open-api-client.default.access-key-id}")
	DefaultClientAccessKeySecret string //starter:inject("${aliyun.open-api-client.default.access-key-secret}")

	myDefaultClient openapi.Client
}

func (inst *AliyunOpenAPIService) _impl() openapi.Service {
	return inst
}

// GetDefaultClient ...
func (inst *AliyunOpenAPIService) GetDefaultClient() (openapi.Client, error) {

	client := inst.myDefaultClient
	if client != nil {
		return client, nil
	}

	cfg, err := inst.getDefaultConfig()
	if err != nil {
		return nil, err
	}

	client, err = inst.NewClient(cfg)
	if err != nil {
		return nil, err
	}

	inst.myDefaultClient = client
	return client, nil
}

// NewClient ...
func (inst *AliyunOpenAPIService) NewClient(cfg *openapi.Configuration) (openapi.Client, error) {

	ua := inst.HTTPClients.NewClient()

	client := new(openAPIClient)
	client.userAgent = ua
	client.config = cfg
	client.parent = inst

	return client, nil
}

func (inst *AliyunOpenAPIService) getDefaultConfig() (*openapi.Configuration, error) {
	cfg := new(openapi.Configuration)

	cfg.Endpoint = inst.DefaultClientEndpoint
	cfg.AccessKeyID = inst.DefaultClientAccessKeyID
	cfg.AccessKeySecret = inst.DefaultClientAccessKeySecret

	return cfg, nil // , fmt.Errorf("no impl")
}
